package org.serverct.finalfight.game.hadesgame;

import org.bukkit.scheduler.BukkitRunnable;
import org.serverct.finalfight.FinalFight;
import org.serverct.finalfight.game.GameManager;
import org.serverct.finalfight.game.hadesgame.events.*;
import org.serverct.finalfight.internal.game.basic.BasicGame;
import org.serverct.finalfight.internal.game.basic.GameState;
import org.serverct.finalfight.internal.game.hadesgame.BasicEvent;
import org.serverct.finalfight.internal.info.SimpleInfoHandler;
import org.serverct.finalfight.util.CustomSettings;
import org.serverct.finalfight.util.StringPair;

public class EventHandler {
    public static double period = CustomSettings.config.getInt("period");
    public static String origin = CustomSettings.getLanguageWithoutPrefix("randomeventbarstring");

    public static BasicEvent randomEvent(BasicGame game) {
        return callEvent(game, EventType.values()[(int) (EventType.values().length * Math.random())]);
    }

    public static BasicEvent callEvent(BasicGame game, EventType type) {
        switch (type) {
            case MOREDROPS:
                return new MoreDrops(game, type);
            case VEGETARIAN:
                return new Vegetarian(game, type);
            case CRAZYCOCKTAILS:
                return new CrazyCocktails(game, type);
            case ARROWGROUND:
                return new ArrowGround(game, type);
            case BADMOOD:
                return new BadMood(game, type);
            case BROKENINVENTORY:
                return new BrokenInventory(game, type);
            case EXCHANGEPOSITION:
                return new ExchangePosition(game, type);
            case RANDOMFRIEND:
                return new RandomFriend(game, type);
            case SKYTRACKER:
                return new SkyTracker(game, type);
            case SMOOTHSURFACE:
                return new SmoothSurface(game, type);
            case TANTRUMMINGMOON:
                return new TantrummingMoon(game, type);
            case TANTRUMMINGSUN:
                return new TantrummingSun(game, type);
        }
        return null;
    }

    public static void executeRandomEvents(BasicGame game) {
        if (!game.enableRandomEvents) {
            return;
        }
        new BukkitRunnable() {
            double origin_sec = game.nextevent_sec;

            @Override
            public void run() {
                if (!GameManager.arenas.containsKey(game.id)) {
                    this.cancel();
                    return;
                }
                if (game.state == GameState.RUNNING) {
                    game.nextevent_sec--;
                    if (game.nextevent_sec <= 0) {
                        game.currentevent = randomEvent(game);
                        game.nextevent_sec = CustomSettings.config.getInt("period") + (int) (Math.random() * 20) - (int) (Math.random() * 20);
                        this.origin_sec = game.nextevent_sec;
                    }
                    SimpleInfoHandler.broadcastActionBar(game, false, "randomeventactionbar", new StringPair("%bar%", this.getBar()));
                }
            }

            public String getBar() {
                double quotient = game.nextevent_sec / this.origin_sec;
                int index = (int) (quotient * origin.length());
                String color = "";
                if (quotient >= 0) {
                    color = CustomSettings.getLanguageWithoutPrefix("randomeventcolor.zero");
                }
                if (quotient >= 0.0625) {
                    color = CustomSettings.getLanguageWithoutPrefix("randomeventcolor.onesixteenth");
                }
                if (quotient >= 0.125) {
                    color = CustomSettings.getLanguageWithoutPrefix("randomeventcolor.oneeighth");
                }
                if (quotient >= 0.25) {
                    color = CustomSettings.getLanguageWithoutPrefix("randomeventcolor.quarter");
                }
                if (quotient >= 0.5) {
                    color = CustomSettings.getLanguageWithoutPrefix("randomeventcolor.half");
                }
                return color + origin.substring(0, index) + "§7§l" + origin.substring(index);
            }
        }.runTaskTimer(FinalFight.plugin, 0L, 20L);
    }
}
